//
//  MeatShopApi.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/21.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatShopApi.h"
#import "Request.h"
#import "RequestHandler.h"
#import "NetworkConfigration.h"
#import "SystemUtil.h"



@implementation MeatShopApi

+ (instancetype)sharedInstance {
    
    static MeatShopApi *sharedInstance = nil;
    static dispatch_once_t token;
    
    dispatch_once(&token,^{
        if(sharedInstance == nil){
            sharedInstance = [[MeatShopApi alloc] init];
        }
    } );
    
    return sharedInstance;
}


#pragma mark - 创建店铺
- (RACSignal<PigTypeModel *> *)getShopCreateByParams:(ShopCreateParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLShopCreate];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 分割方案列表
- (RACSignal <ShopSeparatePlanModel *> *)getShopSeparatePlan{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLShopSperatePlan];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = [ShopSeparatePlanModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 分割方案详情
- (RACSignal <ShopSeparateDetailModel *> *)getShopSeparatePlanDetailByDivisionPlanId:(NSNumber *)divisionPlanId{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLShopSperatePlanDetail, divisionPlanId];
    
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = [ShopSeparateDetailModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 确认分割方案
- (RACSignal<PigTypeModel *> *)getShopSeparatePlanOkByShopId:(NSNumber *)shopId divisionPlanId:(NSNumber *)divisionPlanId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLShopSperatePlanOk];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"shopId":shopId,@"divisionPlanId":divisionPlanId};
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 分配方案（肉块分配）
- (RACSignal<ShopSeparateModel *> *)getShopSparateByParams:(ShopSeparateParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLShopSperatePlanList];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [ShopSeparateModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 预留或销售
- (RACSignal<PigTypeModel *> *)getShopSaveOrSaleByAdvanceSaleType:(NSNumber *)advanceSaleType advanceSaleId:(NSArray*)advanceSaleId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLShopSaleOrSave];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"advanceSaleType":advanceSaleType, @"advanceSaleId":advanceSaleId};
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 我得肉铺
- (RACSignal<PigTypeModel *> *)getMyShopByTribeId:(NSNumber *)tribeId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLShopMine];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":kMemberId,@"tribeId":tribeId};
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 我得店铺销售
- (RACSignal<ShopSeparateModel *> *)getMyShopSaleByParams:(ShopMineSaleParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyShopSale];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [ShopSeparateModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 我得店铺预留
- (RACSignal<ShopSeparateModel *> *)getMyShopSaveByParams:(ShopMineSaleParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyShopSave];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [ShopSeparateModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 上下架与促销
- (RACSignal<NSNull *> *)getMyShopSalePromotionByParams:(ShopSalePromotionParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyShopSalePromotion];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}

#pragma mark - 上商品改价
- (RACSignal<NSNull *> *)getMyShopChagePriceByParams:(ShopChagePriceParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyShopChangePrice];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 店铺账本
- (RACSignal<ShopBookModel *> *)getMyShopBookByShopId:(NSNumber *)shopId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyShopBook];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"shopId":shopId,@"memberId":kMemberId};
    request.responseType = [ShopBookModel class];
    return [self createSignalWithReuqest:request];
}

#warning memberid 是固定的， 为了测试


#pragma mark - 销售明细
- (RACSignal<ShopSaleDetailModel *> *)getMyShopSaleDetailByShopId:(NSNumber *)shopId pigId:(NSNumber *)pigId sellStatus:(NSNumber *)sellStatus{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMySaleDetail];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"shopId":shopId,@"pigId":pigId,@"sellStatus":sellStatus };
    request.responseType = [ShopSaleDetailModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 转让明细_未完成明细
- (RACSignal<PigTypeModel *> *)getMyTranferDetailNOByShopId:(NSNumber *)shopId pigId:(NSNumber *)pigId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMySaleDetail_NO];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"shopId":shopId,@"pigId":pigId};
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 出售明细
- (RACSignal<PigTypeModel *> *)getMySaleDetailByShopId:(NSNumber *)shopId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyBuyDetail];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"shopId":shopId,@"memberId":kMemberId};
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 订单详情
- (RACSignal<PigTypeModel *> *)getMyOrderDetailByOrderId:(NSNumber *)goodsOrderId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyOrderDetail];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"goodsOrderId":goodsOrderId};
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 转让明细_已完成明细
- (RACSignal<PigTypeModel *> *)getMyTranferDetailFinishByShopId:(NSNumber *)shopId pigId:(NSNumber *)pigId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyTranferDetail_Finish];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"shopId":shopId,@"pigId":pigId};
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 转让管理
- (RACSignal<PigTypeModel *> *)getMyTranferManageByShopId:(NSNumber *)shopId pigId:(NSNumber *)pigId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyTranferManager];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"shopId":shopId,@"pigId":pigId, @"memberId":kMemberId};
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 转让管理_改价
- (RACSignal<NSNull *> *)getMyTranferManageChagePriceByAssigShopId:(NSNumber *)assigShopId money:(NSNumber *)money{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyTranferManager_ChagePrice];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"assigShopId":assigShopId,@"money":money};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}

#pragma mark - 转让管理_转让明细
- (RACSignal<PigTypeModel *> *)getMyTranferManageDetailByAssigShopId:(NSNumber *)assigShopId pigId:(NSNumber *)pigId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyTranferManager_Detail];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"assigShopId":assigShopId,@"pigId":pigId, @"memberId":kMemberId};
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 预留_一键赠送
- (RACSignal<PigTypeModel *> *)getMyTranferSaveByAdvanceSaleType:(NSNumber *)advanceSaleType advanceSaleId:(NSArray *)advanceSaleId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMySend_One];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"advanceSaleType":advanceSaleType,@"advanceSaleId":advanceSaleId, };
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 确认赠送
- (RACSignal<PigTypeModel *> *)getMyShopSendComfirmByParams:(ShopSendComfirmParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMySend_Confirm];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 赠送列表
- (RACSignal<PigTypeModel *> *)getMyShopSendListByParams:(ShopSendListParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMySend_List];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 查看赠送
- (RACSignal<PigTypeModel *> *)getSendCheckByShopId:(NSNumber *)shopId goodsOrderId:(NSArray *)goodsOrderId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMySend_Check];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"shopId":shopId,@"goodsOrderId":goodsOrderId };
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 撤回赠送
- (RACSignal<NSNull *> *)getSendCallBackByGoodsOrderId:(NSNumber *)goodsOrderId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMySend_CallBack];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"goodsOrderId":goodsOrderId};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 一键发单
- (RACSignal<PigTypeModel *> *)getMyOrderSendByParams:(ShopOrderSendParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMySend_OneKey];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 全部订单
- (RACSignal<PigTypeModel *> *)getMyOrderWholeByParams:(ShopOrderWholeParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyOrder_Whole];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 全部订单_设置购买人观看权限
- (RACSignal<ShopResetCameraModel *> *)getShopResetByPigId:(NSNumber *)pigId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyOrder_Reset];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"pigId":pigId,@"memberId":kMemberId, };
    request.responseType = [ShopResetCameraModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 预定订单复制
- (RACSignal <ShopOrderCopyModel *> *)getOrderCopyByPigId:(NSNumber *)pigId{
    
    Request *request = [[Request alloc] init];
    
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLMyOrder_Copy, pigId];
    
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = [ShopOrderCopyModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 买家申请更换收货地址.
- (RACSignal<PigTypeModel *> *)getShopChangeLocationByGoodsOrderId:(NSNumber *)goodsOrderId receiveAddress:(NSString *)receiveAddress{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyOrder_ChageLocation];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"goodsOrderId":goodsOrderId,@"receiveAddress":receiveAddress, @"memberId":kMemberId};
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 冷链物流
- (RACSignal <NSNull *> *)getColdLogisticsByGoodsOrderId:(NSNumber *)goodsOrderId{
    
    Request *request = [[Request alloc] init];
    
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLColdLogistics, goodsOrderId];
    
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 好友发单
- (RACSignal <ShopOrderFriendModel *> *)getOrderByShopId:(NSNumber *)shopId{
    
    Request *request = [[Request alloc] init];
    
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@", URLMyOrder_Friend,kMemberId, shopId];
    
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = [ShopOrderFriendModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - Other

- (NSString *)getFullURLString:(NSString *)URLString {
    NetworkConfigration *configration = [NetworkConfigration defaultNetworkConfiguration];
    return [NSString stringWithFormat:@"%@%@", configration.baseApiURL, URLString];
}


- (RACSignal *)createSignalWithReuqest:(Request *) request {
    
    
    return [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        RequestHandler *handler = [[RequestHandler alloc] initWithRequest:request responseCallback:^(Response *response) {
            
            
#pragma mark - 回参数
            if (response.code == ResultCodeSuccess) {
                
                if(request.responseType) {
                    [subscriber sendNext:response.data];
                } else {
                    [subscriber sendNext:[NSNull null]];
                }
                [subscriber sendCompleted];
                
            } else {
                if (response.code == ResultCodeDeviceChanged) {//更换设备
                    UserInfo *userInfo = [UserInfoDataProvider sharedInstance].userInfo;
                    userInfo.isLogin = NO;
                    [UserInfoDataProvider sharedInstance].userInfo = userInfo;
                    
                }
                NSError *error = [NSError errorWithDomain:@"com.naturalgrowth.pigmarket.ErrorDomain" code:response.code userInfo: @{ NSLocalizedDescriptionKey : response.msg }];
                [subscriber sendError:error];
            }
            
        }];
        
        [handler sendRequest];
        
        return [RACDisposable disposableWithBlock:^{
            
            [handler cancelRequest];
        }];
    }] subscribeOn:[RACScheduler scheduler]] ;
}


@end


